<%@ page contentType="text/html;charset=utf-8" language="java" pageEncoding="utf-8"%>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
	<head>
		<META http-equiv=Content-Type content="text/html; charset=utf-8">
		<LINK href="../styles/stylewang.css"  type=text/css rel=stylesheet>
		<style type="text/css"></style>
		<script language=javascript>
			//因为在不用的form中,radio同名也不会相互排斥
			function unselectOthers(rd){
				var radios=document.getElementsByName("selectModel");
				for(var i=0;i<radios.length;i++){
					if(radios[i]==rd){
						radios[i].checked=true;
					}else{
						radios[i].checked=false;
					}
				}
			}
			function showImg(name){
				var a=document.getElementById("modelImg");
				a.setAttribute("src","<%=request.getContextPath() %>/categorySet/images/"+name+".jpg");
			}
			
			function validatenew(){
				if(document.getElementById("modifyName")!=null){
					alert("Modify first");
					document.getElementById("newname").value="";
					return false;
				}
				
				if(document.getElementById("newname").value=="" ){
					alert("Name can not be null");
					return false;
				}
				
				var tds=document.getElementsByTagName("td");
				var names=new Array();
				var types=new Array();
				for(var i=0;i<tds.length;i++){
					if(tds[i].getAttribute("name")=="cateModelName"){
						names.push(tds[i]);
					}
					if(tds[i].getAttribute("name")=="cateModelType"){
						types.push(tds[i]);
					}
				}
				
				var newname=document.getElementById("newname").value;
				var newtype=document.getElementById("newtype").value;
				for(var i=0;i<names.length;i++){
					if(names[i].firstChild.firstChild.nodeValue==newname && types[i].firstChild.nodeValue==newtype){
						alert("Duplicated name!");
						return true;
					}
				}
				return true;
			}
			
			function validatemod(){
				
				if(document.getElementById("modifyName").value=="" ){
					alert("Name can not be null");
					return false;
				}
				var tds=document.getElementsByTagName("td");
				var names=new Array();
				var types=new Array();
				for(var i=0;i<tds.length;i++){
					if(tds[i].getAttribute("name")=="cateModelName"){
						names.push(tds[i]);
					}
					if(tds[i].getAttribute("name")=="cateModelType"){
						types.push(tds[i]);
					}
				}
				
				var newname=document.getElementById("modifyName").value;
				var newtype=document.getElementById("modifyType").value;
				for(var i=0;i<names.length;i++){
					if(names[i].firstChild.nodeValue==newname && types[i].firstChild.nodeValue==newtype){
						alert("Duplicated name!");
						return false;
					}
				}
				return true;
			}
			
			function modify(id){
				var messhot=document.getElementById("messhot");
				var meselement=document.getElementById("meselement");
				var cell=document.getElementById(id);
				var value=cell.firstChild.firstChild.nodeValue;
				if(navigator.appName=="Microsoft Internet Explorer"){
					var desc=cell.nextSibling.nextSibling.firstChild.nodeValue;
					cell.nextSibling.nextSibling.nextSibling.innerHTML="&nbsp;<input type='button' value='Modify' onclick='javascript:if(validatemod()){submit()};'>";
					cell.nextSibling.nextSibling.innerHTML="<textarea name='modeldesc'>"+desc+"</textarea>";
					cell.nextSibling.setAttribute("id","modifyType");
					var submitType=cell.nextSibling.firstChild.nodeValue;
					cell.nextSibling.innerHTML+="<input type='hidden' name='cateModelType' value='"+submitType+"'>";
					cell.innerHTML="<input id='modifyName' type='text' name='cateModelName' value="+value+">";
				}else{
					var a=cell.nextSibling.nextSibling.nextSibling.nextSibling;
					var desc=a.firstChild.nodeValue;
					a.nextSibling.nextSibling.innerHTML="<input type='button' value='Modify' onclick='javascript:if(validatemod()){submit1("+value+");}'>";
					a.innerHTML="<textarea name='modeldesc'>"+desc+"</textarea>";
					cell.nextSibling.nextSibling.setAttribute("id","modifyType");
					var submitType=cell.nextSibling.nextSibling.firstChild.nodeValue;
					cell.nextSibling.nextSibling.innerHTML+="<input type='hidden' name='cateModelType' value='"+submitType+"'>";
					cell.innerHTML="<input id='modifyName' type='text' name='cateModelName' value="+value+">";
				}
				
			}
			
			function submit1(vl){
	//			alert(vl);
	//			var form=document.getElementById(vl);
				alert(vl.getAttribute("id"));
				vl.submit();
			 }
			 function addRelation(){
			 	var radios=document.getElementsByName("selectModel");
			 	var modelId="";
			 	var flag=false;
			 	if(navigator.appName=="Microsoft Internet Explorer"){
			 		for(var i=0;i<radios.length;i++){
			 			if(radios[i].checked==true){
			 				modelId=radios[i].parentNode.nextSibling.getAttribute("id");
			 				flag=true;
			 				break;
			 			}
			 		}
			 	}else{
			 		for(var i=0;i<radios.length;i++){
			 			if(radios[i].checked==true){
			 				modelId=radios[i].parentNode.nextSibling.nextSibling.getAttribute("id");
			 				flag=true;
			 				break;
			 			}
			 		}
			 	}
			 	if(!flag){
			 		alert("请先选择模版!");
			 		return ;
			 	}
			 	window.location.href="../gantt/modeladdunit.do?catemodelid="+modelId+"&ObjectID=<%=request.getParameter("ObjectID") %>&Type=<%=request.getParameter("Type") %>" ;
			 }
		</script>
	</head>
	
	<body>
		<input type="hidden" id="messhot" value="<bean:message key='shot'/>">
		<input type="hidden" id="meselement" value="<bean:message key='element'/>">
<!-- 	<frameset id="modelframe"  frameborder="NO" border="0" framespacing="0">  -->
		<%
		String relation=null;
		if(request.getParameter("ganttrelation")!=null){
			relation=request.getParameter("ganttrelation");
		}else if(request.getAttribute("ganttrelation")!=null){
			relation=(String)request.getAttribute("ganttrelation");
		}else{
			relation="0";
		}
		
		String gantttype=null;
		if(request.getParameter("Type")!=null){
			gantttype=request.getParameter("Type");
		}else if(request.getAttribute("gantttype")!=null){
			gantttype=(String)request.getAttribute("gantttype");
		}else{
			gantttype="0";
		}
	 %>	
	<table border="0" cellpadding="0" cellspacing="0" width="50%">
		<tr>
		<td>
		<table  border="0" width="100%" cellspacing="0" id="table1">
		<tr>
			<td width="15%" id="btleftbian"><b>&nbsp;<bean:message key="model"/></b></td>
			<td width="15%" id="btcenterbian"><b>&nbsp;<bean:message key="modelname"/></b></td>
			<td width="15%" id="btcenterbian"><b>&nbsp;<bean:message key="modeltype"/></b></td>
			<td width="15%" id="btcenterbian"><b>&nbsp;<bean:message key="modeldes"/></b></td>
			<td width="15%" id="btrightbian">&nbsp; </td>
		</tr>
		<logic:iterate id="element" name="models">
		<tr>
		<form action="modifymodel.do" method="POST" id="<bean:write name="element" property="cateModelName"/>">
			<td id="leftcenter"><input type="radio" name="selectModel" onClick="showImg('<bean:write name="element" property="cateModelId"/>');unselectOthers(this);"></td>
			<td id="<bean:write name="element" property="cateModelId"/>" name="cateModelName"  width="15%" class="leftcenter"><html:link page="/modeldetail.do" paramId="cateModelId" paramName="element" paramProperty="cateModelId"><bean:write name="element" property="cateModelName"/></html:link></td>
			<td name="cateModelType" width="26%" class="leftcenter"><bean:write name="element" property="cateModelType"/></td>
			<td name="cateModeldesc" width="15%" class="leftcenter"><bean:write name="element" property="description"/>&nbsp;</td>
			<td width="29%" id="rightcenter">
				<a style="cursor:hand" onClick="modify(<bean:write name="element" property="cateModelId"/>)">[<bean:message key="cate.modify"/>]</a>
				<%if(relation.equals("1")){ if(gantttype.equals("Element")){%>
				
				<html:link page="/delmodel.do?ganttrelation=1&Type=Element&" paramId="cateModelId" paramName="element" paramProperty="cateModelId"  onclick="return confirm('')">[<bean:message key="cate.delete"/>]</html:link>
				<%}if(gantttype.equals("Shot")){ %>
				<html:link page="/delmodel.do?ganttrelation=1&Type=Shot&" paramId="cateModelId" paramName="element" paramProperty="cateModelId"  onclick="return confirm('')">[<bean:message key="cate.delete"/>]</html:link>
				<%} %>
				<%}else{ %>
				<html:link page="/delmodel.do?" paramId="cateModelId" paramName="element" paramProperty="cateModelId"  onclick="return confirm('')">[<bean:message key="cate.delete"/>]</html:link>
				<%} %>
			</td>
		<%if(relation.equals("1")){%><input type="hidden" name="ganttrelation" value="1"><%} %>	
		<input type="hidden" name="cateModelId" value="<bean:write name='element' property='cateModelId'/>">
		</form>
		</tr>
		</logic:iterate>
		<form action="createmodel.do?" method="POST" >
		<tr>
			<td id="leftcenter">&nbsp;</td>
			<td width="15%" id="leftcenter">&nbsp;<input id="newname" type="text" name="modelname" ></td>
			<%if(relation.equals("1")){ %>
			<td width="15%" id="leftcenter">&nbsp;<select id="newtype" name="modeltype" ><option value="<%=gantttype %>"><%=gantttype %></option></select></td>
			<%}else{ %>
			<td width="15%" id="leftcenter">&nbsp;<select id="newtype" name="modeltype" ><option value="Element"><bean:message key="element"/></option><option value="Shot"><bean:message key="shot"/></option></select></td>
			<%} %>
			<td width="15%" id="leftcenter">&nbsp;<textarea id="newdesc" name="modeldesc" ></textarea></td>
			<td width="15%" id="rightcenter">&nbsp;<input type="button" value="New" onClick="javascript:if(validatenew()){submit();}">
			<%if(request.getAttribute("notnull")!=null){%><span STYLE="color:red"><%=request.getAttribute("notnull") %></span><%} %>
			<%if(request.getAttribute("dup")!=null){ %><span STYLE="color:red"><%=request.getAttribute("dup") %></span><%} %>
			</td>
		</tr>
		<tr><Td id="tdtop" colspan="5">&nbsp;</Td></tr>
		</table>
		<table cellspacing="0" cellpadding="0"><tr><td>
			<%if(relation.equals("1")){ %>
				<input type="button" onClick="addRelation();" value="addrelation"><br>
				&nbsp; <a href="<%=basePath %>categorySet/index.do"><bean:message key="setcategory"/></a>
			<%}else{ %>
			<input type="button" value="back" onClick="window.location.href='index.do';">
			<%} %>
		</td></tr></table>
		<%if(relation.equals("1")){%><input type="hidden" name="ganttrelation" value="1"><%} %>
		</form>
		</td>
		<td>
		<div scrolling="yes" ><img id="modelImg" src=""></div>
		</td>
		</tr>
		</table>
<!--  	</frameset> -->
	</body>
</html>